﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESHOP.Domain;
using System.Data.Common;

namespace ESHOP.DAO
{
   public class DanhMucDAO :BaseDAO
    {
       
       public DANHMUC Get(String ma)
       {
           return _db.DANHMUCs.Where(p => p.ID.Equals( ma)).SingleOrDefault();
       }
       public List<DANHMUC> GetList()
       {
           return _db.DANHMUCs.ToList();
       }
       public int Update(DANHMUC objui)
       {
           int result = 1;
           try
           {
               var objdb = Get(objui.ID);
               if (objdb != null)
               {
                   objdb.TENDM = objui.TENDM;
                   _db.SubmitChanges();
               }
           }
           catch
           {
               result = 0;
           }
           return result;
       }
       public int Insert(DANHMUC objiu)
       {
           int result = 1;
           DbTransaction trans = null;
           try
           {
               _db.Connection.Open();
               trans = _db.Connection.BeginTransaction();
               _db.Transaction = trans;
               _db.DANHMUCs.InsertOnSubmit(objiu);
               _db.SubmitChanges();
               trans.Commit();
           }
           catch
           {
               result = 0;
           }

           return result;
       }
       public int Delete(String ma)
       {
           int result = 1;
           var objdb = Get(ma);
           try
           {
               _db.DANHMUCs.DeleteOnSubmit(objdb);
               _db.SubmitChanges();
           }
           catch
           {
               result = 0;
           }
           return result;
       }
    }
}
